package com.ym.manager.yinsheng.util;

import com.ym.manager.admin.properties.Config;
import com.ym.utils.JsonUtil;
import com.ym.utils.Logger;
import com.ym.utils.StringHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 客户端
 * @Author linxl
 * @Date 2021/4/1 9:18
 * @Version 1.0
 */
public class YinShengClient {

	private String merchantId;
	/**
	 * 接口名称
	 */
	private String api;
	/**
	 * 环境
	 */
	private String env;

	private String url;
	/**
	 * 证书路径
	 */
	private String privateCerPath;

	/**
	 * 私钥证书密码
	 */
	private String privateCerPwd;

	private ServiceEnum serviceEnum;


	/**
	 * @作者: dhf
	 * @名称:调用银盛接口
	 * @param requestData
	 * @时间: 2022/8/9 20:49
	 * @备注:
	 * @返回值: java.lang.String
	 */
	public Map<String, Object> requestApi(RequestData requestData,String msg){
		Map<String,Object> put = new HashMap<>();
		requestData.serviceNo = serviceEnum.getApiName();
		requestData.srcMerchantNo = this.merchantId;
		Map<String, String> map = YsSignUtils.buildResponseSign(this.privateCerPwd,this.privateCerPath,requestData);
		Logger.info("YinShengClient---url:"+this.url+"---postData-----"+JsonUtil.toJson(map));
		String result = HttpClient.post(this.url,map);
		if (StringUtils.isBlank(result)) {
			Logger.error("银盛渠道"+msg+"返回数据为空");
			put.put("code",500);
			put.put("msg",msg+"错误");
			return put;
		}
		Logger.info("YinShengClient------"+msg+"result-----"+result);
		Map<String,Object> obj = JsonUtil.jsonToBean(result,Map.class);
		String code = StringHandler.getString(obj.get("code"),"");
		if(!code.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛响应成功.getCode())){
			Logger.error("银盛渠道"+msg+"返回错误:"+obj.get("msg"));
			put.put("code",500);
			put.put("msg",obj.get("msg"));
			return put;
		}
		if(StringHandler.isEmpty(obj.get("bizResponseJson"))){
			Logger.error("银盛渠道返"+msg+"回错误:bizResponseJson为空");
			put.put("code",500);
			put.put("msg",msg+"错误");
			return put;
		}
		Map<String,Object> bizResponseJson = JsonUtil.jsonToBean(JsonUtil.toJson(obj.get("bizResponseJson")),Map.class);
		String subCode = StringHandler.getString(bizResponseJson.get("subCode"),"");
		if("recharge".equals(requestData.serviceNo) || "withdraw".equals(requestData.serviceNo) || "consume".equals(requestData.serviceNo)){
			String subMsg = StringHandler.getString(bizResponseJson.get("subMsg"));
			if(subCode.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛业务响应成功.getCode())){
				obj.putAll(bizResponseJson);
				obj.put("code",0);
				return obj;
			}else if(subCode.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛业务响应处理中.getCode()) && subMsg.contains("处理中")){
				obj.putAll(bizResponseJson);
				obj.put("code",0);
				return obj;
			}else{
				if(!subCode.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛业务响应成功.getCode())){
					Logger.error("银盛渠道"+msg+"返回错误:"+bizResponseJson.get("subMsg"));
					put.put("code",500);
					put.put("msg",bizResponseJson.get("subMsg"));
					return put;
				}
			}
		}else{
			if(!subCode.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛业务响应成功.getCode())){
				Logger.error("银盛渠道"+msg+"返回错误:"+bizResponseJson.get("subMsg"));
				put.put("code",500);
				put.put("msg",bizResponseJson.get("subMsg"));
				return put;
			}
		}
		obj.putAll(bizResponseJson);
		return obj;
	}
	public String requestStrApi(RequestData requestData){
		requestData.serviceNo = serviceEnum.getApiName();
		requestData.srcMerchantNo = this.merchantId;
		Map<String, String> map = YsSignUtils.buildResponseSign(this.privateCerPwd,this.privateCerPath,requestData);
		String result = HttpClient.post(this.url,map);
		Logger.info("YinShengClient------result-----"+result);
		if (StringUtils.isBlank(result)) {
			Logger.error("银盛渠道返回数据为空");
			return "";
		}
		return result;
	}
	public YinShengClient(ServiceEnum serviceEnum) {
		this.merchantId = StringHandler.getString(Config.pp.get("ys.merchant.id"),"826691148160089");
		this.serviceEnum = serviceEnum;
		this.api = serviceEnum.getApiName();
		this.privateCerPath = StringHandler.getString(Config.pp.get("ys.merchant.pfx.path"),"/opt/app/ys-web-server/"+merchantId+".pfx");
//		this.privateCerPath = "D://"+merchantId+".pfx";
		this.privateCerPwd = StringHandler.getString(Config.pp.get("ys.merchant.pfx.pwd"),"23847672");
		this.env = YsPayConstant.ENV_PRD;
		this.url = serviceEnum.getTestUrl();
		if (YsPayConstant.ENV_PRD.equals(env)) {
			this.url = serviceEnum.getPrdUrl();
		}
	}
}
